﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Data.UserResponse;
using ThreeByte.ActiveDeck.Config;

namespace ThreeByte.ActiveDeck.AddIn.Content.UserResponse
{
    /// <summary>
    /// Interaction logic for CategoriesDataUserControl.xaml
    /// </summary>
    public partial class CategoriesDataUserControl : UserControl
    {
        public CategoriesDataUserControl() {
            InitializeComponent();
        }

        public CategoriesConfig Config { get; set; }


        public void Refresh(CategoriesUserData userData) {

            LayoutRoot.Children.Clear();
            LayoutRoot.ColumnDefinitions.Clear();

            int currentColumn = -1;
            foreach(CategoryData cat in userData.Categories) {
                //Add a Column
                currentColumn++;

                //Get the category name from the slide config
                TextBlock categoryTitle = new TextBlock() { Text = Config.Categories[cat.ID - 1].Text };
                categoryTitle.SetValue(Grid.ColumnProperty, currentColumn);
                categoryTitle.SetValue(Grid.RowProperty, 0);
                LayoutRoot.Children.Add(categoryTitle);

                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());
                StackPanel newStack = new StackPanel();
                newStack.Margin = new Thickness(20);
                newStack.SetValue(Grid.ColumnProperty, currentColumn);
                newStack.SetValue(Grid.RowProperty, 1);
                LayoutRoot.Children.Add(newStack);

                //TODO: Make this more flexible
                for(int i = 0; i < 10 && i < cat.ItemValues.Count; i++) {
                    CategoryItemValue newValue = cat.ItemValues[i];
                    CategoriesItemElement newItem = new CategoriesItemElement();
                    newItem.Word = Config.Words[newValue.ID - 1].Text;
                    newItem.Percent = newValue.Percent;
                    //newItem.Color = Brushes.Green;
                    //newItem.ColorInvert = Brushes.Red;

                    switch(currentColumn) {
                        case 0:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FFF95100")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#3FF95100")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Right;
                            newItem.PercentView2.Visibility = Visibility.Collapsed;
                            newItem.PercentBarLeft.Visibility = Visibility.Visible;
                            break;
                        case 1:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FFFBFF00")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#41DEE112")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Center;
                            newItem.PercentBarCenter.Visibility = Visibility.Visible;
                            break;
                        case 2:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FF00FF16")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#3F21A02C")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Left;
                            newItem.PercentView1.Visibility = Visibility.Collapsed;
                            newItem.PercentBarRight.Visibility = Visibility.Visible;
                            break;

                    }
                   


                    newStack.Children.Add(newItem);
                }

            }


        }

    }
}
